package display {
	import com.adobe.serialization.json.JSON;
	
	import flare.animate.Easing;
	import flare.animate.Transitioner;
	import flare.vis.Visualization;
	import flare.vis.data.DataSprite;
	import flare.vis.data.EdgeSprite;
	import flare.vis.data.NodeSprite;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.aswing.AsWingManager;
	import org.aswing.BoxLayout;
	import org.aswing.Container;
	import org.aswing.FlowLayout;
	import org.aswing.JComboBox;
	import org.aswing.JFrame;
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	import org.aswing.SoftBoxLayout;
	import org.aswing.event.SelectionEvent;
	
	import util.GraphMLUtil;

	public class ControlBoardSprite extends Sprite {
		public var frame:JFrame;
		public var dropDownList:JComboBox;
		private var trash:Array = [null, null, null, {nodes:[], edges:[]}];
		private var vis_:Visualization;
		public function ControlBoardSprite(vis:Visualization, stage:Stage) {
			super();
			vis_ = vis;
			AsWingManager.setRoot(stage);
			frame = new JFrame(null, 'Control board');

			frame.setClosable(false);
			frame.setResizable(false);
			frame.setDragable(false);
			
			var pane:Container = frame.getContentPane();
						pane.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
//			pane.setLayout(new FlowLayout(FlowLayout.LEFT));
			
			dropDownList = new JComboBox();
			var text:Array = [
				'Gmail Friend View',
				'Yahoo Friend View',
				'MSN Friend View',
				'Full Friend View'
			];
			dropDownList.setListData(text);
			dropDownList.getPopupList().addSelectionListener(onSelect);

//			dropDownList.setWidth(100);
			dropDownList.setWidth(150);

			var label:JLabel = new JLabel("Switch view");
			var panel:JPanel = new JPanel(new BoxLayout(BoxLayout.X_AXIS));
			panel.append(label);
			panel.append(dropDownList);
			panel.setHeight(50);
			pane.append(panel);
			
			trace('ControlBoardSprite >' + pane.getChildAt(0));
			trace('ControlBoardSprite >' + JSON.encode(pane));
			frame.setX(0);
			frame.setY(335);
			frame.setSizeWH(230, 70);
			frame.show();
		}
		private function onSelect(event:SelectionEvent):void {
			var index:int = event.getLastIndex();
			switch (index) {
				case 0:
					trace('ControlBoardSprite > here');
					switchToGmailView();
					break;
				case 1:
					switchToYahooView();
					break;
				case 2:
					switchToMSNView();
					break;
				case 3:
					swithToFullView();
					break;
			}
//			vis_.update(null, "nodes", "edges", "main");
		}
		private function reloadDataFromAsset(filename:String, hightlight:Boolean):void {
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest(filename);
			var stage:Stage = this.stage;

			loader.addEventListener(Event.COMPLETE, function(event:Event):void {
				var l:URLLoader = event.target as URLLoader;
				var xml:XML = new XML(l.data);
				GraphMLUtil.initDataWithGraphMLXML(xml, vis_, hightlight);
				vis_.update(null, "nodes", "edges", "main", "layout");
			});
			loader.load(request);
			
		}
		private function selectGroup(group:int):void {
			var t:Transitioner = new Transitioner(1, Easing.easeInBounce);
			var o:Object;
			vis_.data.nodes.visit(function(n:NodeSprite):void {
				o = t.$(n);
				if (n.data.nodegroup == group) {
					o.alpha = 1;
					o.visible = true;
				} else {
					o.alpha = 0;
					o.visible = false;
				}
			});
			vis_.data.edges.visit(function(e:EdgeSprite):void {
				o = t.$(e);
				if (e.source.data.nodegroup == group && e.target.data.nodegroup == group) {
					o.visible = true;
				} else {
					o.visible = false;
				}
			});
			t.play();
		}
		private function swithToFullView():void {
//			for (var i:int = 0; i < 3; ++i) {
//				if (trash[i]) restoreFromTrash(i);
//			}

//			reloadDataFromAsset("resources/dataset.graphml", true);

			var t:Transitioner = new Transitioner(1, Easing.easeInBounce);
			var o:Object;
			vis_.data.nodes.visit(function(n:NodeSprite):void {
				o = t.$(n);
				o.visible = true;
				o.alpha = 1;
			});
			vis_.data.edges.visit(function(e:EdgeSprite):void {
				o = t.$(e);
				o.visible = true;
				o.alpha = 0;
			});
			t.play();
		}
		private function switchToGmailView():void {
//			trace('ControlBoardSprite >', trash[0]);
//			if (trash[0]) {
//				restoreFromTrash(0);
//			}
//			if (trash[1] == null) {
//				trace('here1');
//				removeToTrash(1);
//			}
//			if (trash[2] == null) {
//				trace('here2');
//				removeToTrash(2);
//			}
			
//			trace(JSON.encode(trash));
//			reloadDataFromAsset("resources/gmail.graphml", false);
			selectGroup(0);
		}
		private function switchToYahooView():void {
//			if (trash[1]) {
//				restoreFromTrash(1);
//			}
//			if (trash[0] == null) {
//				removeToTrash(0);
//			}
//			if (trash[2] == null) {
//				removeToTrash(2);
//			}
//			reloadDataFromAsset("resources/yahoo.graphml", false);
			selectGroup(1);
		}
		private function switchToMSNView():void {
//			if (trash[2]) {
//				restoreFromTrash(2);
//			}
//			if (trash[0] == null) {
//				removeToTrash(0);
//			}
//			if (trash[1] == null) {
//				removeToTrash(1);
//			}
//			reloadDataFromAsset("resources/msn.graphml", false);
			selectGroup(2);
		}
		private function restoreFromTrash(group:int):void {
			var t:Object = trash[group];
			var n:DataSprite;
			var e:EdgeSprite;
			if (t.nodes) {
				for each (n in t.nodes) {
					vis_.data.nodes.add(n);
					n.dirty();
				}
			}
			if (t.edges) {
				for each (n in t.edges) {
					vis_.data.edges.add(n);
					n.dirty();
				}
			}
			if (trash[3].edges) {
				for each (e in trash[3].edges) {
					if (e.source.data.nodegroup == group && e.target.data.nodegroup == group) {
					vis_.data.edges.add(e);
					n.dirty();
					}
				}
			}
			if (group == 3) {
				trash[group].edges = [];
				trash[group].nodes = [];
			} else {
				trash[group] = null;	
			}
			
		}
		private function removeToTrash(group:int):void {
			trash[group] = {nodes: [], edges: []};
			var t:Object = trash[group];
			var edge:EdgeSprite;
			var node:NodeSprite;
			for each (edge in vis_.data.edges) {
				if (edge.source.data.nodegroup == group && edge.target.data.nodegroup == group) {
					t.edges.push(edge);
				} else {
					trash[3].edges.push(edge);
				}
			}
			for each (node in vis_.data.nodes) {
				if (node.data.nodegroup == group) {
//					vis_.data.nodes.remove(node);
					t.nodes.push(node);
				}
			}
			for each (edge in t.edges) {
				vis_.data.removeEdge(edge);
//				edge.dirty();
			}
			for each (node in t.nodes) {
				vis_.data.removeNode(node);
//				node.dirty();
			}
			trace('t >', JSON.encode(t));
		}
		/*
		public function update(text:Array):void {
			trace('ControlBoardSprite >' + JSON.encode(text));
			dropDownList.setListData(text);
			frame.show();
		}*/
	}
}
